define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'playlet.playlet/index' + location.search,
                    add_url: 'playlet.playlet/add',
                    edit_url: 'playlet.playlet/edit',
                    del_url: 'playlet.playlet/del',
                    multi_url: 'playlet.playlet/multi',
                    import_url: 'playlet.playlet/import',
                    table: 'playlet',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'cover', title: __('Cover'), operate: 'LIKE', formatter: Table.api.formatter.image},
                        {field: 'thumbs', title: __('Thumbs')},
                        {field: 'comment', title: __('Comment')},
                        {field: 'ishot', title: __('Ishot')},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {field: 'category', title: __('Category')},
                        {field: 'year', title: __('Year')},
                        {field: 'sum', title: __('Sum')},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"hidden": __('Hidden'), "normal": __('Normal')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: function (data) {
                                        return '【' + data.name + '】 章节';
                                    },
                                    text: '章节',
                                    classname: 'btn  btn-xs btn-primary   btn-dialog',
                                    icon: 'fa fa-list-ol',
                                    extend: 'data-area=\'["900px", "90%"]\'',
                                    url: 'playlet/playlet_chapter/index'
                                }
                            ]

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
